package ao.util.io;

import org.apache.log4j.Logger;

import java.io.*;
import java.util.Arrays;
import java.util.Collections;

/**
 * Date: Oct 17, 2008
 * Time: 3:13:25 PM
 */
public class Slurpy
{
    //-------------------------------------------------------------------------
    private static final Logger LOG =
            Logger.getLogger( Slurpy.class );


    //-------------------------------------------------------------------------
    private Slurpy() {}


    //-------------------------------------------------------------------------
    public static boolean write(
            String contents, String fileName)
    {
        return write( contents, new File( fileName ) );
    }

    public static boolean write(
            String contents, File outFile)
    {
        try
        {
            boolean closeFailed[] = {false};
            doWrite(contents, outFile, closeFailed);
            return !closeFailed[0];
        }
        catch (IOException e)
        {
            LOG.warn("Unable to write", e);
            return false;
        }
    }

    private static void doWrite(
            String  contents,
            File    outFile,
            boolean closeFailed[])
                throws IOException
    {
        Dirs.pathTo( outFile );

        OutputStream out = null;
        try
        {
            out = new FileOutputStream( outFile );
            out.write( contents.getBytes() );
        }
        finally
        {
            if (out != null)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                    closeFailed[0] = true;
                    LOG.warn("Unable to close file", e);
                }
            }
        }
    }


    //-------------------------------------------------------------------------
    public static String read(String contentsOf)
    {
        return read( new File( contentsOf ) );
    }
    public static String read(File contentsOf)
    {
//        return slurp(contentsOf);
//          return new String(PersistentBytes.retrieve(contentsOf));

        if (! contentsOf.canRead()) return null;

        InputStream fis = null;
        try
        {
            fis = new BufferedInputStream(
                    new FileInputStream(contentsOf));
            return doSlurpString( contentsOf, fis );
        }
        catch (IOException e)
        {
            throw new Error( e );
        }
        finally
        {
            if (fis != null)
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static String doSlurpString(
            File        contentsOf,
            InputStream fis) throws IOException
    {
        StringBuilder str = new StringBuilder();

        for (int i = (int)(contentsOf.length() - 1);
                 i >= 0;
                 i--) {
            str.append((char) fis.read());
        }

        return str.toString();
    }


    //-------------------------------------------------------------------------
    public static Iterable<String> readLines(String file)
    {
        return readLines(new File(file));
    }

    public static Iterable<String> readLines(
            final File file)
    {
        String flat = read(file);
        if (flat == null) return Collections.emptyList();

        return Arrays.asList(
                 flat.split("(\\r\\n|\\n)"));
        

//        return new Iterable<String>() {
//            @Override public Iterator<String> iterator() {
//                BufferedReader reader = null;
//                try
//                {
//                    reader = new BufferedReader(new FileReader(file));
//                    final BufferedReader finalReader = reader;
//                    return new Iterator<String>() {
//                        private String nextLine = finalReader.readLine();
//                        @Override public boolean hasNext() {
//                            return (nextLine != null);
//                        }
//
//                        @Override public String next() {
//                            String prevNExtLine = nextLine;
//                            try {
//                                nextLine = finalReader.readLine();
//                            } catch (IOException e) {
//                                throw new Error( e );
//                            }
//                            return prevNExtLine;
//                        }
//
//                        @Override
//                        public void remove() {
//                            throw new UnsupportedOperationException();
//                        }
//                    };
//                }
//                catch (IOException e)
//                {
//                    throw new Error( e );
//                }
//                finally
//                {
//                    if (reader != null)
//                    {
//                        try
//                        {
//                            reader.close();
//                        }
//                        catch (IOException e)
//                        {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//        };
    }
}
